'''
Author: moe_wang wangmeng@oceancreate.com
Date: 2022-10-27 09:19:40
LastEditors: moe_wang wangmeng@oceancreate.com
LastEditTime: 2022-10-27 11:14:03
FilePath: \generate-monthly-shift-plan\monthPlanV.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
# -*- coding: UTF-8 -*-
import os
import datetime
from datetime import timedelta
import csv

def genDates(b_date, days):
    day = timedelta(days=1)
    for i in range(days):
        yield b_date + day*i

def getDateList(startDate, endDate):
    """
    获取日期列表
    :param start: 开始日期
    :param end: 结束日期
    :return:
    """
    if startDate is not None:
        start = datetime.datetime.strptime(startDate, "%Y-%m-%d")
    if endDate is None:
        end = datetime.datetime.now()
    else:
        end = datetime.datetime.strptime(endDate, "%Y-%m-%d")
    data = []
    for d in genDates(start, ((end-start).days + 1)):
        data.append(d.strftime("%m.%d"))
    return data

def getListExcludeMondy(list):
    result = []
    temp = []
    for index in range(len(list)):
        if (index % 7) == 0:
            temp = []
        elif (index % 7) ==6:
            temp.append(list[index])
            result.append(temp)
        else:
            temp.append(list[index])
            
    return result

def getFirstCourseIndex(course):
    index = allCourse.index(course)
    return index + 1
 
# 课程字典表
classPlanDictionary = {
    '快乐家园':['巧巧的餐厅','森林奇遇','宝贝爱读书','梦幻小屋','相框','场景游戏','哆唻咪','小车快跑','超市购物','小小消防员','沙滩旅行','小兔子蹦蹦跳'],
    '管道工程':['木盒抢球赛','摇摇摆摆','滑滑梯','小小探险家','高山流水','安全小行家','失踪的小球','投篮宝贝','猜猜看','淘气的章鱼','喷泉','拍照小人'],
    '自然奥秘':['可爱的毛毛虫','旋转萨利','三只小猪','聪明的小海龟','搬运比赛','高高的长颈鹿','花开满园','我为城市做贡献','小小邮递员','水开了','我爱洗澡','动物园'],
    '运动与探索':['小袋鼠蹦蹦跳','一起去郊游','疯狂的迷宫','障碍赛跑','我爱游泳','会唱歌的盒子','小小救护车','我是小小特种兵','天鹅湖','百变的轮子','空中运输','推土机'],
    '百变梦工厂':['大吊车','空中表演','钻孔车','农用飞机','城市清洁工','空中救援','发射火箭','去旅行','豪华游轮','海底寻宝记','火星探秘','吊起来'],
    '城市之旅':['高楼大厦','我爱看电影','城市公交','幼儿园','安全出行','地铁','病房','人行天桥','发现王国','一起去爬山','神奇发电站','金字塔的奥秘'],
    '梦想建筑师上':['小人国防御战','老虎拔牙','搜救公主','博士的烦恼','轮滑表演','小猪吃苹果','守卫城堡','顽皮的小鳄鱼','我的旅行箱','摇头摆尾','牢固的椅子','轿运车'],
    '梦想建筑师下':['多功能梯子','怪异的球门','灯塔','扬帆起航','拱桥','MANDY的礼物','汽车总动员','安全卫士','有趣的不倒翁','旋转木马','疯狂小人','衣柜'],
}

# 暴露入参 上月每班最后一次课程
preMonthLastCourse = ['城市清洁工','可爱的毛毛虫','木盒抢球赛','可爱的毛毛虫','猜猜看','空中表演','三只小猪','博士的烦恼','三只小猪','高高的长颈鹿','三只小猪','花开满园']

# 暴露入参 起止日期 变量
startDate = '2022-10-31'
endDate = '2022-12-04'

# 暴露入参 当月排班周
currentMonthCourseWeek = [2,3,3,3,4,5,6,6,6,7,7,7]

# 当月每周日期 5周,每周内为周二至周日
currentMonthWeeklyDateArray = getListExcludeMondy(getDateList(startDate, endDate))
# 当月每班课程
currentMonthCourseIndex = []
currentMonthCourse = []
# 所有课程一维数组
allCourse = []
# 最终输出数组
currentMonthCourseWithDate = []

for key in classPlanDictionary:
    allCourse = allCourse + classPlanDictionary[key]

for item in preMonthLastCourse:
        index = getFirstCourseIndex(item)
        currentMonthCourseIndex.append([index,index + 1,index + 2,index + 3,index + 4])
        currentMonthCourse.append([allCourse[index],allCourse[index + 1],allCourse[index + 2],allCourse[index + 3],allCourse[index + 4]])   
        
for classIndex in range(len(currentMonthCourse)):
    # print(classIndex)
    currentMonthCourseWithDate.append([])
    oneClass = currentMonthCourse[classIndex]
    for week in range(len(oneClass)):
        # print('week:',week)
        classText = oneClass[week]
        date = currentMonthWeeklyDateArray[week][currentMonthCourseWeek[classIndex]-2]
        classText = date+classText
        currentMonthCourseWithDate[classIndex].append(classText)

print(currentMonthCourseWithDate)

if os.path.exists('./temp.csv'):
    os.remove('./temp.csv')

with open('temp.csv','w',newline='') as file:
    writer = csv.writer(file)
    writer.writerows(currentMonthCourseWithDate)